package com.brayan.webapp.service.realestate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;

import com.brayan.webapp.model.realestate.Reservation;

/**
 * JSON object which will be forwarded to the GUI.
 * 
 * @author Brayan
 * 
 */
public class CalendarDay {
	
	private static Logger LOGGER = Logger.getLogger(CalendarDay.class);

	final String DATE_FORMAT = "yyyy-MM-dd";

	private List<ReservationShort> reservationList = new ArrayList<ReservationShort>();
	private String nameOfTenant = "";
	private LocalDateShort date;

	public List<ReservationShort> getReservationList() {
		return reservationList;
	}

	public void addToReservationList(Reservation reservation) {
		this.reservationList.add(new ReservationShort(reservation));
	}

	public void removeFromReservationList(ReservationShort reservation) {
		this.reservationList.remove(reservation);
	}

	public LocalDateShort getDate() {
		return date;
	}

	public void setDate(LocalDate localDate) {
		this.date = new LocalDateShort(localDate);
	}

	/**
	 * Returns the GMT Date object (not CET).
	 * 
	 * @return
	 */
	public Date getDateObject() {
		Date localSystemDate = null;
		DateTime dateTime = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
			sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
			localSystemDate = sdf.parse(this.getDateString());
			dateTime = new DateTime(localSystemDate, DateTimeZone.UTC);
			// 2014-04-16T16:51:31.195Z
			LOGGER.trace("CalendarDay date UTC: " + dateTime);
		} catch (ParseException e) {
			LOGGER.error("Failed to parse date from CalendarDay object. " + getDateString());
		}
		if (dateTime != null)
			return dateTime.toDate();
		else{
			LOGGER.error("No Date (null) available for this CalendarDay object. " + getDateString());
			return null;
		}
			
	}

	public String getDateString() {
		return getDate().toString();
	}

	public String getNameOfTenant() {
		return nameOfTenant;
	}

	public void setNameOfTenant(String nameOfTenant) {
		this.nameOfTenant = nameOfTenant;
	}

	private class ReservationShort {
		ReservationShort(Reservation reservation) {
			reservationId = reservation.getId();

			DateFormat df = new SimpleDateFormat(DATE_FORMAT);
			reservationStart = df.format(reservation.getStartDate());
			reservationEnd = df.format(reservation.getEndDate());
			if (reservation.getTenant() != null)
				nameOfTenant = reservation.getTenant().toString();
			reservationState = reservation.getReservationState().toString();
		}

		public Long reservationId;
		public String reservationStart;
		public String reservationEnd;
		public String nameOfTenant;
		public String reservationState;
	}

}
